package core.september.cloudbadge.vcard.badgemodel;

import javax.persistence.Id;

import com.google.gson.Gson;

import core.september.cloudbadge.vcard.badgemodel.fields.AbsField;
import core.september.cloudbadge.vcard.badgemodel.fields.HomeField;
import core.september.cloudbadge.vcard.badgemodel.fields.NameField;
import core.september.cloudbadge.vcard.badgemodel.fields.PhotoField;
import core.september.cloudbadge.vcard.badgemodel.fields.WorkingField;
import ezvcard.VCard;

public class UserVcard  {

	private String vcard;
	private HomeField hf;
	private WorkingField wf;
	private NameField nf;
	private PhotoField pf;
	
	@Id
	private String id;
	

	public UserVcard() {}
	
	public String getId() {
		return id;
	}
	
	public VCard getVcard() {
		if(vcard == null) {
			setVcard(new VCard());
		}
		Gson gson = new Gson();
		return gson.fromJson(vcard, VCard.class); 
	}


	public void setVcard(VCard vcard) {
		Gson gson = new Gson();
		this.vcard = gson.toJson(vcard);
	}


	public HomeField getHf() {
		if(hf == null) {
			setHf(new HomeField());
		}
		return hf;
	}


	public void setHf(HomeField hf)  {
		this.hf = hf;
		this.hf.setVcard(getVcard());
	}


	public WorkingField getWf() {
		if(wf == null) {
			setWf(new WorkingField());
		}
		return wf;
	}


	public void setWf(WorkingField wf) {
		this.wf = wf;
		this.wf.setVcard(getVcard());
	}
	
	public NameField getNf() {
		if(nf == null) {
			setNf(new NameField());
		}
		return nf;
	}


	public void setNf(NameField nf) {
		this.nf = nf;
		this.nf.setVcard(getVcard());
	}


	public PhotoField getPf() {
		if(pf == null) {
			setPf(new PhotoField());
		}
		return pf;
	}


	public void setPf(PhotoField pf) {
		this.pf = pf;
		this.pf.setVcard(getVcard());
	}
	
	public void commit() {
		AbsField[] components = new AbsField[]{hf,wf,nf,pf};
		for(AbsField field: components) {
			if(field != null) {
				field.commit();
			}
		}
	}


}
